//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SwaptionPseudoDerivative.h"
using namespace Cephei::QL::Models::Marketmodels::Pathwisegreeks;
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (Cephei::QL::Models::Marketmodels::IMarketModel^ inputModel, UInt64 startIndex, UInt64 endIndex) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMarketModel^ _CinputModel;
    try
    {
#ifdef HANDLE
        _phSwaptionPseudoDerivative = NULL;
#endif
        _CinputModel = safe_cast<CMarketModel^> (inputModel);
        _CinputModel->Lock();
        boost::shared_ptr<QuantLib::MarketModel> _inputModel = static_cast<boost::shared_ptr<QuantLib::MarketModel>> (_CinputModel->GetShared ()); 
        QuantLib::Size _startIndex = (QuantLib::Size)ValueHelper::Convert (startIndex);
        QuantLib::Size _endIndex = (QuantLib::Size)ValueHelper::Convert (endIndex);
        _ppSwaptionPseudoDerivative = new boost::shared_ptr<QuantLib::SwaptionPseudoDerivative> (new QuantLib::SwaptionPseudoDerivative ( _inputModel,  _startIndex,  _endIndex ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CinputModel != nullptr) _CinputModel->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (boost::shared_ptr<QuantLib::SwaptionPseudoDerivative>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwaptionPseudoDerivative = NULL;
#endif
	_ppSwaptionPseudoDerivative = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (QuantLib::SwaptionPseudoDerivative& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSwaptionPseudoDerivative = NULL;
#endif
	_ppSwaptionPseudoDerivative = new boost::shared_ptr<QuantLib::SwaptionPseudoDerivative> (&childNative);
    
    _SwaptionPseudoDerivativeOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (CSwaptionPseudoDerivative^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwaptionPseudoDerivative = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwaptionPseudoDerivative = new boost::shared_ptr<QuantLib::SwaptionPseudoDerivative> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwaptionPseudoDerivative = NULL;
#endif
	if (!t->IsSubclassOf(CSwaptionPseudoDerivative::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (QuantLib::Handle<QuantLib::SwaptionPseudoDerivative>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSwaptionPseudoDerivative = &childNative;
	_ppSwaptionPseudoDerivative = &static_cast<boost::shared_ptr<QuantLib::SwaptionPseudoDerivative>>(childNative.currentLink());
    
    _SwaptionPseudoDerivativeOwner = owner;
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (QuantLib::Handle<QuantLib::SwaptionPseudoDerivative> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSwaptionPseudoDerivative = &childNative;
	_ppSwaptionPseudoDerivative = &static_cast<boost::shared_ptr<QuantLib::SwaptionPseudoDerivative>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::CSwaptionPseudoDerivative (QuantLib::SwaptionPseudoDerivative childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwaptionPseudoDerivative = NULL;
#endif
	_ppSwaptionPseudoDerivative = new boost::shared_ptr<QuantLib::SwaptionPseudoDerivative> (new QuantLib::SwaptionPseudoDerivative (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::~CSwaptionPseudoDerivative ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSwaptionPseudoDerivative != NULL)
    {
	    delete _ppSwaptionPseudoDerivative;
        _ppSwaptionPseudoDerivative = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::!CSwaptionPseudoDerivative ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSwaptionPseudoDerivative != NULL)
    {
	    delete _ppSwaptionPseudoDerivative;
    }
}
QuantLib::SwaptionPseudoDerivative& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::GetReference ()
{
    if (_ppSwaptionPseudoDerivative == NULL) throw gcnew NativeNullException ();
	return **_ppSwaptionPseudoDerivative;
}
boost::shared_ptr<QuantLib::SwaptionPseudoDerivative>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::GetShared ()
{
    if (_ppSwaptionPseudoDerivative == NULL) throw gcnew NativeNullException ();
	return *_ppSwaptionPseudoDerivative;
}
QuantLib::SwaptionPseudoDerivative* Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::GetPointer ()
{
    if (_ppSwaptionPseudoDerivative == NULL) throw gcnew NativeNullException ();
	return &**_ppSwaptionPseudoDerivative;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwaptionPseudoDerivative>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::GetHandle ()
{
	if (_phSwaptionPseudoDerivative == NULL)
	{
		_phSwaptionPseudoDerivative = new Handle<QuantLib::SwaptionPseudoDerivative> (*_ppSwaptionPseudoDerivative);
	}
	return *_phSwaptionPseudoDerivative;
}
#endif
bool Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::HasNative () 
{
	return (_ppSwaptionPseudoDerivative != NULL);
}

Double Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::Expiry::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwaptionPseudoDerivative)->expiry ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::ImpliedVolatility::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwaptionPseudoDerivative)->impliedVolatility ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::Variance::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwaptionPseudoDerivative)->variance ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::VarianceDerivative (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppSwaptionPseudoDerivative)->varianceDerivative ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative::VolatilityDerivative (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppSwaptionPseudoDerivative)->volatilityDerivative ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Pathwisegreeks::ISwaptionPseudoDerivative^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CSwaptionPseudoDerivative_Factory::Create (Cephei::QL::Models::Marketmodels::IMarketModel^ inputModel, UInt64 startIndex, UInt64 endIndex)
{
    return gcnew CSwaptionPseudoDerivative ( inputModel,  startIndex,  endIndex);
}
